class Solution {
public:
    ListNode* swapPairs(ListNode* head) {
        if(head == NULL || head->next == NULL)
        {
            return head;
        }
        ListNode* dummyNode = new ListNode(-1, head);
        ListNode* prev = head;
        ListNode* cur = head->next;
        ListNode* pprev = dummyNode;
        ListNode* newhead = NULL;
        if(cur->next == NULL)
        {
            prev->next = cur->next;
            cur->next = prev;
            pprev->next = cur;
            newhead = cur;
            return newhead;
        }
        while(cur && cur->next)
        {
            if(prev == head)
            {
                prev->next = cur->next;
                cur->next = prev;
                pprev->next = cur;
                newhead = cur;
                pprev = prev;
                prev = prev->next;
                cur = prev->next;
            }
            else if(prev && cur)
            {
                prev->next = cur->next;
                cur->next = prev;
                pprev->next = cur;
                pprev = prev;
                prev = prev->next;
                cur = prev->next;
            }
        }
        if(cur)
        {
            prev->next = cur->next;
            cur->next = prev;
            pprev->next = cur;
        }
        return newhead;
    }
};